package com.chatgpt;

import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;

public class ChatGPTExample1 {

    public static void main(String[] args) throws IOException {
        OkHttpClient client = new OkHttpClient();

        MediaType mediaType = MediaType.get("application/json");
        JSONObject json = new JSONObject();
        json.put("model", "gpt-3.5-turbo");

        // 构造 messages
        json.put("messages", new org.json.JSONArray()
                .put(new JSONObject().put("role", "user").put("content", "你好,我想在淞沪路这边找一个3000的房子")));

        RequestBody body = RequestBody.create(json.toString(), mediaType);
        Request request = new Request.Builder()
                .url("https://api.openai.com/v1/chat/completions")
                .post(body)
                .addHeader("Authorization", "Bearer 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")
                .addHeader("Content-Type", "application/json")
                .build();

        Response response = client.newCall(request).execute();
        System.out.println(response.body().string());
    }
}
